/* 接口地址：http://jx.xuzhixiang.top/ap/api/cart-list.php
接口请求方式：get
接口参数：
     id  用户id

使用方式
     获取id为1的用户的购物车
     http://jx.xuzhixiang.top/ap/api/cart-list.php?id=1

服务器返回json数据
132159 */
var top3 = document.querySelector('.top3');
async function cartlist() {
    let uid = localStorage.getItem('uid');
    let cartAddAPI = 'http://jx.xuzhixiang.top/ap/api/cart-list.php';
    let { data } = await axios.get(cartAddAPI, { params: { id: uid } });
    let cartArr = data.data;

    let liarr = cartArr.map(v =>
        `
    <div class="top3-1">
    <input type="checkbox" class="dan">
        <img src="${v.pimg}" alt="">
    <p>${v.pname}</p>
    <span class="dj">￥${v.pprice}</span>
    <div class="dian">
        <button class="jian">-</button>
        <input type="text" class="inp" name="" id="" value="${v.pnum}">
        <button class="zeng" >+</button>
    </div>
    <div class="bz">
    <span class="biao">￥</span><span class="zj">${v.pprice * v.pnum}</span></div>
    <span class="del" onclick="fn(${v.pid})">删除</span>
    </div>
    `
    )
    top3.innerHTML = liarr.join('');
    zd()
    // jj()

}
cartlist();
/* 接口地址：http://jx.xuzhixiang.top/ap/api/cart-delete.php
接口请求方式：get
接口参数：
     uid  用户id
     pid  商品id

使用方式
     删除用户1中的 id为1商品
     http://jx.xuzhixiang.top/ap/api/cart-delete.php?uid=1&pid=1

服务器返回json数据 */
async function fn(pid) {
    let uid = localStorage.getItem('uid');
    let cartDelAPI = 'http://jx.xuzhixiang.top/ap/api/cart-delete.php';
    let res = await axios.get(cartDelAPI, { params: { uid, pid } });
    console.log(res);
    let del = document.querySelector('.del');
    top3.removeChild(del.parentNode)
}
//全选单选
function zd() {
    var all = document.querySelector('.all');
    var dan = document.querySelectorAll('.dan');
    // console.log(dan);
    all.onclick = function () {
        console.log(all);
        console.log(dan);
        for (i = 0; i < dan.length; i++) {
            dan[i].checked = all.checked;
        }
    }
    for (i = 0; i < dan.length; i++) {
        dan[i].onclick = function () {
            console.log(dan);
            var ji = true;
            for (i = 0; i < dan.length; i++) {
                if (!dan[i].checked) {
                    ji = false;
                    break;
                }
            }
            all.checked = ji;
        }
    }
}
//加减
/* function jj() {
    var inp = document.querySelectorAll('.inp')
    var zeng = document.querySelectorAll('.zeng');
    var jian = document.querySelectorAll('.jian');
    for (var i = 0; i < inp.length; i++) {
        inp[i].onblur = function () {
            var value = this.value;
            this.value = value
        }
    }
    for (var i = 0; i < inp.length; i++) {
        zeng[i].onclick = function () {
            // var index=i;
            var value = inp[i].value;
            value++
            inp[i].value = value
        }
    }
    for (var i = 0; i < inp.length; i++) {
        jian[i].onclick = function () {
            var index = i;
            var value = inp[index].value;
            value--
            if (value < 1) {
                value = 1
            }
            inp[index].value = value
        }
    }
}
 */
